import requests
from datetime import datetime
from AbstractClass.AbstractGetter import AbstractProvider


class Provider(AbstractProvider):
    def __init__(self, cookie: str):
        super().__init__()
        self.cookie = cookie

    def get_data(self, iframe_content="", frame_content="", dom=None, extra=None):
        # 获取当前学年与学期
        now_date = datetime.now()
        year = now_date.year
        month = now_date.month
        xqm = 3 if month >= 8 else 4
        xnm = year if xqm == 3 else year - 1  # 第二学期取去年的年份

        try:
            # 构造请求参数
            post_data = f"xnm={xnm}&xqm={xqm}"
            headers = {
                "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
                "cookie": self.cookie,
            }

            # 发送POST请求
            response = requests.post(
                    "http://jwjxnew.njit.edu.cn/jwglxt/kbcx/xskbcx_cxXsgrkb.html",
                    headers=headers,
                    data=post_data,
            )
            res = response.json()

        except Exception as error:
            # 错误处理（可根据需要实现AIScheduleAlert的Python版本）
            print(f"获取课程信息时发生错误，{error}")
            raise error

        # 构造返回结果
        result = {
            "data": res,
            "xnm": xnm,
            "xqm": xqm
        }
        return result
